﻿using Fuse8.DomainFramework.BusinessService;
using Fuse8.DomainFramework.Domain;
using Fuse8.GlobalizationFramework.Domain;
using Fuse8.GlobalizationFramework.Infrastructure;

namespace Fuse8.GlobalizationFramework.BusinessServices
{
    /// <summary>
    /// Base Business Service for Globalizabel entities
    /// </summary>
    /// <typeparam name="TDomain"></typeparam>
    /// <remarks>
    /// When Adding it sets current culture. When created it sets PublisGroupId based on Id. When Saving it check current culture and adds first new item.
    /// </remarks>
    public class GlobalizableBusinessService<TDomain> : BaseBusinessService<TDomain, int>
        where TDomain : class, IEntityObject<int>, IGlobalizableEntity, new()
    {
        /// <summary>
        /// Raises the <see cref="E:Fuse8.DomainFramework.BusinessService.BaseBusinessService`2.Creating"/> event before saves data to the repository.
        /// </summary>
        /// <remarks>
        /// The OnCreating() method also enables derived classes to handle the event without attaching a delegate. 
        ///             This is the preferred technique for handling the event in a derived class.
        /// </remarks>
        protected override void OnCreating(EntityPreEventArgs<TDomain, int> e)
        {
            base.OnCreating(e);

            GlobalizationMediator.InitCulture(e.Entity);
        }

        /// <summary>
        /// Raises the <see cref="E:Fuse8.DomainFramework.BusinessService.BaseBusinessService`2.Created"/> event after saves data to the repository.
        /// </summary>
        /// <remarks>
        /// The OnCreated() method also enables derived classes to handle the event without attaching a delegate. 
        ///             This is the preferred technique for handling the event in a derived class.
        /// </remarks>
        protected override void OnCreated(EntityEventArgs<TDomain, int> e)
        {
            base.OnCreated(e);

            if (GlobalizationMediator.IsDefaultLanguage())
            {
                e.Entity.PublicGroupId = e.Entity.Id;
                Save(e.Entity);
            }
        }

        /// <summary>
        /// Raises the <see cref="E:Fuse8.DomainFramework.BusinessService.BaseBusinessService`2.Updating"/> event before saves data to the repository.
        /// </summary>
        /// <remarks>
        /// The OnUpdating() method also enables derived classes to handle the event without attaching a delegate. 
        ///             This is the preferred technique for handling the event in a derived class.
        /// </remarks>
        protected override void OnUpdating(EntityPreEventArgs<TDomain, int> e)
        {
            base.OnUpdating(e);

            GlobalizationMediator.InitCulture(e.Entity);

            if (!GlobalizationMediator.IsDefaultLanguage())
            {
                if (!e.Entity.Translated)
                {
                    e.Entity.Id = 0;
                    Add(e.Entity);
                    e.Cancel = true;
                }
            }
        }
    }
}